package com.xcity.game.shop.mystery.op;

import java.text.MessageFormat;

import com.xcity.db.entity.Player;
import com.xcity.game.common.AbstractConfirmOperation;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.item.ItemTemplate;
import com.xcity.game.shop.mystery.PlayerMysteryShopItem;
import com.xcity.pomelo.push.msg.TipPush;

/**
 * 废弃(客户端自己处理二次确认)
 * @author yang.li
 *
 */
@Deprecated
public class MysteryShopBuyOperation extends AbstractConfirmOperation {
	
	protected PlayerMysteryShopItem item;

	public MysteryShopBuyOperation(Player ref, PlayerMysteryShopItem item) {
		super(ref, 
				MessageFormat.format(MessageConst.SHOP_BUY_TIP, 
						item.getTemplate().getPrice() * item.getCount(),
						"钻石",
						item.getCount(),
						item.getName())
				);
		this.item = item;
	}

	@Override
	public void ok(Player player) {
		if (item.getState() == PlayerMysteryShopItem.STATE_SELL_OUT) {
			player.send(new TipPush(MessageConst.SHOP_ITEM_SOLD_OUT));
			return;
		}
		ItemTemplate template = item.getTemplate().getRef();
		int needBagGridSize = template.isAutoUse() ? 0 : (item.getCount() + template.getStack() - 1) / template.getStack();
		if (player.getBag().getFreeSize() < needBagGridSize) {
			player.send(new TipPush(MessageConst.BAG_FULL));
			return;
		}
		if (player.costStone(item.getTemplate().getPrice() * item.getCount(), Cause.MYSTERY_SHOP_BUY)) {
			item.setState(PlayerMysteryShopItem.STATE_SELL_OUT);
			player.getBag().addItemWithoutException(item.getId(), item.getCount(), Cause.MYSTERY_SHOP_BUY);
		}
	}

}
